package com.cs157a.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.cs157a.controller.AposApplicationController;
import com.cs157a.dao.Make;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * This class shows the dialog for input new auto.
 * 
 * @author hhong
 * 
 */
public class NewMakerDialog extends JDialog {

	private static final long serialVersionUID = 20012100301L;
	private static final int FIELD_WIDTH = 20;
	private AposApplicationController controller;
	private JButton saveButton;
	private JButton cancelButton;
	private JLabel labelMNO;
	private JTextField textFieldMNO;
	private JLabel labelMAK;
	private JTextField textFieldMAK;
	private JLabel labelCOD;
	private JTextField textFieldCOD;

	public NewMakerDialog(final AposApplicationController controller) {
		super();
		this.controller = controller;

		setPreferredSize(new Dimension(400, 200));

		saveButton = new JButton(CommonString.SAVE);
		saveButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (textFieldMAK.getText().isEmpty()
						|| textFieldMAK.getText().length() > 50
						|| textFieldCOD.getText().isEmpty()
						|| textFieldCOD.getText().length() > 3) {
					JOptionPane
							.showMessageDialog(rootPane,
									"Please Enter Maker, Code with Maximum 3 Charaters!");
				} else {
					Make maker = getMaker();
					controller.saveMaker(maker);
					setVisible(false);
					clearAllField();
				}
			}
		});

		cancelButton = new JButton(CommonString.CANCEL);
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				clearAllField();
			}
		});

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);

		add(createInnerPanel(), BorderLayout.NORTH);
		add(buttonPanel, BorderLayout.SOUTH);
		setModalityType(ModalityType.APPLICATION_MODAL);
	}

	protected void clearAllField() {
		// TODO Auto-generated method stub
		textFieldMNO.setText(controller.getNextMakerNo());

		textFieldMAK.setText("");
		textFieldCOD.setText("");
	}

	protected Make getMaker() {
		Make maker = new Make();
		try {
			int n = Integer.parseInt(textFieldMNO.getText().trim());
			maker.setNo(n);
			maker.setName(textFieldMAK.getText().trim());
			maker.setCode(textFieldCOD.getText().trim());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(rootPane, "Error Input", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		return maker;
	}

	private JPanel createInnerPanel() {

		labelMNO = new JLabel(MakerColumn.NO.getName());
		textFieldMNO = new JTextField(FIELD_WIDTH);
		textFieldMNO.setText(controller.getNextMakerNo());
		textFieldMNO.setEditable(false);

		labelMAK = new JLabel(MakerColumn.NAME.getName());
		textFieldMAK = new JTextField(FIELD_WIDTH);

		labelCOD = new JLabel(MakerColumn.CODE.getName());
		textFieldCOD = new JTextField(FIELD_WIDTH);

		clearAllField();

		JPanel innerPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHWEST;
		c.fill = GridBagConstraints.HORIZONTAL;

		innerPanel.add(labelMNO, c);

		c.gridx = 0;
		c.gridy = 1;

		innerPanel.add(labelMAK, c);

		c.gridx = 0;
		c.gridy = 2;
		innerPanel.add(labelCOD, c);

		c.gridx = 1;
		c.gridy = 0;
		innerPanel.add(textFieldMNO, c);

		c.gridx = 1;
		c.gridy = 1;
		innerPanel.add(textFieldMAK, c);

		c.gridx = 1;
		c.gridy = 2;
		innerPanel.add(textFieldCOD, c);

		return innerPanel;
	}
}
